<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * Contao Open Source CMS
 * Copyright (C) 2005-2010 Leo Feyer
 *
 * Formerly known as TYPOlight Open Source CMS.
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, please visit the Free
 * Software Foundation website at <http://www.gnu.org/licenses/>.
 *
 * PHP version 5
 * @copyright  certo web & design GmbH 2011 
 * @author     Yanick Witschi <yanick.witschi@certo-net.ch> 
 * @package    rapidmail_sync_recipients 
 * @license    LGPL
 * @filesource
 */


/**
 * Table tl_newsletter_recipients 
 */
// remove toggle operation
unset($GLOBALS['TL_DCA']['tl_newsletter_recipients']['list']['operations']['toggle']);

// Synch on delete
$GLOBALS['TL_DCA']['tl_newsletter_recipients']['config']['ondelete_callback'][] = array('tl_newsletter_recipients_rm', 'synchOnDelete');

// Synch on add
$GLOBALS['TL_DCA']['tl_newsletter_recipients']['config']['onsubmit_callback'][] = array('tl_newsletter_recipients_rm', 'synchOnAdd');

// Synch on edit
$GLOBALS['TL_DCA']['tl_newsletter_recipients']['fields']['email']['save_callback'][] = array('tl_newsletter_recipients_rm', 'synchOnEdit');

// Synch on active state change
$GLOBALS['TL_DCA']['tl_newsletter_recipients']['fields']['active']['save_callback'][] = array('tl_newsletter_recipients_rm', 'synchOnActiveStateChange');


class tl_newsletter_recipients_rm extends Backend
{
	
	/**
	 * Initialize the object and import the syncher
	 */
	public function __construct()
	{
		parent::__construct();
		$this->import('RapidmailRecipientsSynchronizer');
	}
	
	
	/**
	 * Delete the recipient on rapidmail.de
	 * @param object
	 */
	public function synchOnDelete(DataContainer $dc)
	{
		$strEmail		= $dc->activeRecord->email;
		$intChannelId	= $dc->activeRecord->pid;

		$this->RapidmailRecipientsSynchronizer->removeRecipientFromRapidmail($strEmail, array($intChannelId));
	}
	
	
	/**
	 * Synch on add
	 * @param object
	 */
	public function synchOnAdd(DataContainer $dc)
	{
		$strEmail		= $dc->activeRecord->email;
		$intChannelId	= $dc->activeRecord->pid;

		// if tstamp is 0 it's a new entry
		if($dc->activeRecord->tstamp == 0 && $dc->activeRecord->active == 1)
		{
			$this->RapidmailRecipientsSynchronizer->sendNewRecipientToRapidmail($strEmail, null, array($intChannelId));
		}
	}
	
	
	/**
	 * Synch on edit
	 * @param string
	 * @param object
	 * @return string
	 */
	public function synchOnEdit($varValue, DataContainer $dc)
	{
		// if tstamp is 0 it's a new entry and we don't do anything here
		if($dc->activeRecord->tstamp == 0)
		{
			return $varValue;
		}
		
		$strEmail		= $dc->activeRecord->email;
		$intChannelId	= $dc->activeRecord->pid;
		
		// check if there are any changes
		if($dc->activeRecord->email != $varValue)
		{
			$this->RapidmailRecipientsSynchronizer->removeRecipientFromRapidmail($strEmail, array($intChannelId));
			$this->RapidmailRecipientsSynchronizer->sendNewRecipientToRapidmail($varValue, null, array($intChannelId));
		}
		
		return $varValue;
	}
	
	
	/**
	 * Synch on active state change
	 * @param mixed
	 * @param object
	 * @return string
	 */
	public function synchOnActiveStateChange($varValue, DataContainer $dc)
	{
		// if tstamp is 0 it's a new entry and we don't do anything here
		if($dc->activeRecord->tstamp == 0)
		{
			return $varValue;
		}
		
		$strEmail		= $dc->activeRecord->email;
		$intChannelId	= $dc->activeRecord->pid;
		
		// check if there are any changes
		if($dc->activeRecord->active != $varValue)
		{
			if($varValue == 1)
			{
				$this->RapidmailRecipientsSynchronizer->sendNewRecipientToRapidmail($strEmail, null, array($intChannelId));
			}
			else
			{
				$this->RapidmailRecipientsSynchronizer->removeRecipientFromRapidmail($strEmail, array($intChannelId));
			}			
		}

		return $varValue;
	}
}

?>